﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.WhitestripWeighing
{
    public class WhiteStripWeighingUpdateViewHandler : IRequestHandler<WhiteStripWeighingUpdateViewCommond, Resqults>
    {

        private readonly IBateRepository<WhiteNotes> _WhiteNote;
        private 
            readonly  IBateRepository<LiveAnimalweighing> LiveAnimalweighing;

        public WhiteStripWeighingUpdateViewHandler(IBateRepository<WhiteNotes> whiteNote, IBateRepository<LiveAnimalweighing> liveAnimalweighing)
        {
            _WhiteNote = whiteNote;
            LiveAnimalweighing = liveAnimalweighing;
        }

        public async Task<Resqults> Handle(WhiteStripWeighingUpdateViewCommond request, CancellationToken cancellationToken)
        {
            

            var list=new Resqults() { Code = 0 };

            var query=_WhiteNote.GetAll().Where(x=>x.WhiteNoteid==request.WhiteNoteid).FirstOrDefault();

            if (query!=null)
            {
                query.WhiteNoteWeight = request.WhiteNoteWeight;
                query.WhiteNoteid = request.WhiteNoteid;
                query.WhiteNoteTime = request.WhiteNoteTime;
                query.NetWeight = request.NetWeight;
                query.SlaughterNumber = request.SlaughterNumber;
                query.WhiteNoteRemark = request.WhiteNoteRemark;
                query.WhiteNoteNum = request.WhiteNoteNum;
                query.WhiteNoteTare = request.WhiteNoteTare;



              var success=  _WhiteNote.Update(query);

                if(success!=null)
                {
                    var Hids= LiveAnimalweighing.GetAll().Where(x=>x.LiveAnimalweighingId==request.WhiteWeightid).FirstOrDefault();

                    Hids.WeightNum -= request.WhiteNoteNum;

                var successOne=  LiveAnimalweighing.Update(Hids);
                    if (successOne!=null)
                    {
                        list.Code = 1;
                    }
                }
            }


            return list;




        }
    }
}
